//
//  ManageFeeViewController.m
//  wtshoppingcite
//
//  Created by TW on 15-2-10.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "ManageFeeViewController.h"
#import "BaseTableView.h"
#import "ManageFeeDetailViewController.h"

#import "ManageFeeInfoViewController.h"

@interface ManageFeeViewController ()<UITableViewEventsDelegate,UITableViewDelegate,UITableViewDataSource,manageFeeDelegate>

@property(nonatomic,strong)UITableView *headTableView;

@property(nonatomic,strong)NSArray *data;
@property(nonatomic,strong)ManageFeeModel *manageFeeModel;
@property(nonatomic,strong)RoomModel *roomModel;
@property(nonatomic,strong)UIButton *confirmButton;

@end

@implementation ManageFeeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleView:@"选择住所"];
    
    UILabel *titleLabel = [self createLabelWithFrame:CGRectMake(20, 20, kWidth, 20) text:@"选择您需要缴费的住所" font:13.0f textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
    [self.view addSubview:titleLabel];
    
    _headTableView = [[UITableView alloc]initWithFrame:CGRectMake(20, titleLabel.bottom, kWidth-40, 100) style:UITableViewStylePlain];
    
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth-40, 100)];
    bgView.image = [UIImage imageNamed:@"wuyekuang.png"];
    _headTableView.backgroundView = bgView;
    
    _headTableView.delegate = self;
    _headTableView.dataSource = self;
    _headTableView.scrollEnabled = NO;
    [self.view addSubview:_headTableView];
    
    _confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(10, _headTableView.bottom+20, kWidth-20, 40)];
    [_confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_confirmButton setBackgroundImage:[UIImage imageNamed:@"register_login"] forState:UIControlStateNormal];
    [_confirmButton addTarget:self action:@selector(confirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_confirmButton];
    
    _data = @[@"楼栋",@"房号"];
    
    // Do any additional setup after loading the view.
}


#pragma mark 确认
-(void)confirmButtonAction{
    
    __weak ManageFeeViewController *weak_self = self;
    if (_manageFeeModel.buildingName.length == 0 || [_manageFeeModel.buildingName isEqualToString:@"(null)"] || [_manageFeeModel.buildingName isEqualToString:@"<null>"]) {
        [weak_self showToastMessage:@"请先选择楼栋"];
        return;
    }
    
    if (_roomModel.unitName.length == 0 || [_roomModel.unitName isEqualToString:@"(null)"] || [_roomModel.unitName isEqualToString:@"<null>"]) {
        [weak_self showToastMessage:@"请先选择房间"];
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"%@\n%@",_manageFeeModel.buildingName,_roomModel.unitName];
    if (self.delegate && [self.delegate respondsToSelector:@selector(manageAddress:manageFeeModel:roomModel:)]) {
        [self.delegate manageAddress:str manageFeeModel:_manageFeeModel roomModel:_roomModel];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _data.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"manageCell1"];
        if (cell == nil) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"manageCell1"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.backgroundView = nil;
            cell.contentView.backgroundColor = [UIColor clearColor];
            cell.backgroundColor = [UIColor clearColor];
            
            UILabel *label = [self createLabelWithFrame:CGRectMake(10, 20, 60, 20) text:@"" font:18 textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
            label.tag = 100;
            [cell.contentView addSubview:label];
            
            UILabel *textLabel = [self createLabelWithFrame:CGRectMake(label.right, 20, 220, 20) text:@"" font:18 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
            textLabel.tag = 101;
            [cell.contentView addSubview:textLabel];
            
        }
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:100];
    label.text = _data[indexPath.row];
    
    
    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:101];
    
    
    if (indexPath.row == 0) {
        
        textLabel.text = _manageFeeModel.buildingName;
    }
    
    if (indexPath.row == 1) {
        textLabel.text = _roomModel.unitName;
    }

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ManageFeeDetailViewController *manageFeeDetailVC = [[ManageFeeDetailViewController alloc]init];
    manageFeeDetailVC.hidesBottomBarWhenPushed = YES;
    manageFeeDetailVC.delegate = self;
    
    if (indexPath.row == 0) {
        
        manageFeeDetailVC.index = 0;
        [self.navigationController pushViewController:manageFeeDetailVC animated:YES];
        
    }else if (indexPath.row == 1){
        
       
        __weak ManageFeeViewController *weak_self = self;
        if (_manageFeeModel.buildingName.length == 0 || [_manageFeeModel.buildingName isEqualToString:@"(null)"] || [_manageFeeModel.buildingName isEqualToString:@"<null>"]) {
            [weak_self showToastMessage:@"请先选择楼栋"];
            return;
        }
        manageFeeDetailVC.index = 1;
        manageFeeDetailVC.buildingId = _manageFeeModel.manageFeeId;
        [self.navigationController pushViewController:manageFeeDetailVC animated:YES];
        
    }
}

#pragma mark manageFeeDetail- Delegate
-(void)manageFeeBuild:(ManageFeeModel *)manageFeeModel{
    
    _manageFeeModel = manageFeeModel;
    [_headTableView reloadData];
}

-(void)manageFeeRoom:(RoomModel *)manageFeeRoom{
    
    _roomModel = manageFeeRoom;
    [_headTableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
